/*
 * 	Additional function for tables.html
 *	Written by ThemePixels	
 *	http://themepixels.com/
 *
 *	Copyright (c) 2012 ThemePixels (http://themepixels.com)
 *	
 *	Built for Amanda Premium Responsive Admin Template
 *  http://themeforest.net/category/site-templates/admin-templates
 */

jQuery(document).ready(function(){
	jQuery(".chzn-select").chosen();
	jQuery("#cardDate").datepicker({ 
		dateFormat: "yy-mm-dd",
		changeYear: true,
		changeMonth: true,
		beforeShow: function(i,e){
		   var z = jQuery(i).closest(".ui-dialog").css("z-index") + 4;
		   e.dpDiv.css('z-index', z);
		}});
	
	jQuery('#timecardTable').dataTable({
		"sPaginationType": "full_numbers",
		"oLanguage": {
			"sLengthMenu": "每页显示 _MENU_ 条记录",
			"sZeroRecords": "抱歉， 没有找到",
			"sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
			"sInfoEmpty": "没有数据",
			"sInfoFiltered": "(从 _MAX_ 条数据中检索)",
			"sSearch":"全文搜索:",
			"oPaginate": {
			"sFirst": "首页",
			"sPrevious": "前一页",
			"sNext": "后一页",
			"sLast": "尾页",
			},
			"sZeroRecords": "没有检索到数据",
			"sProcessing": "<img src='./loading.gif' />"
		}
	});
	
	
	jQuery('#addTimecardBtn').click(function(){
		jQuery('#timecard_dialog').attr("title","添加考勤信息");
		jQuery('#timecardForm').attr("action","/timecard/tableAdd");
		jQuery('#empId_select').val('').trigger('liszt:updated');
		jQuery('#edit_btn').hide();
		jQuery('#add_btn').show();
		jQuery("#timecard_dialog").dialog({
			 width:500,
		     modal:true
		});
	});
	
	jQuery("#cardName").change(function(){
		var cardName = jQuery("#cardName").val();
		var empId = jQuery("#empId_select").val();
		if(cardName == '' || empId == '') {
			return;
		}
		jQuery.ajax({
	    	type:"post",
	    	dataType:"json",
	    	url:"/timecard/calCardMoney",
	    	data:{cardName:cardName, empId:empId},
	    	success:function(data) {
	    		jQuery("#cardMoney").val(data.cardMoney);
	    	}
	    });
	});
	
	jQuery("#empId_select").change(function(){
		var cardName = jQuery("#cardName").val();
		var empId = jQuery("#empId_select").val();
		if(cardName == '' || empId == '') {
			return;
		}
		jQuery.ajax({
	    	type:"post",
	    	dataType:"json",
	    	url:"/timecard/calCardMoney",
	    	data:{cardName:cardName, empId:empId},
	    	success:function(data) {
	    		jQuery("#cardMoney").val(data.cardMoney);
	    	}
	    });
	});
});

function showEditDialog(id){
	jQuery.ajax({
		url:'/timecard/cardInfo',
		type:'POST',
		async:true,
		data:{
			id:id
		},
		dataType:'json',
		success:function(data){
			if(data.result.status == "success") {
				var timecard = data.result.timecard;
				jQuery('#timecard_dialog').attr("title","修改考勤信息");
				jQuery('#timecardForm').attr("action","/timecard/tableEdit");
				jQuery('#edit_btn').show();
				jQuery('#add_btn').hide();
				jQuery("#cardId").val(timecard.cardId);
				jQuery("#cardName").val(timecard.cardName);
				jQuery("#cardMoney").val(timecard.cardMoney);
				jQuery("#cardDate").val(timecard.cardDate);
				jQuery("#cardId").val(timecard.cardId);
				jQuery("#comment").html(timecard.cardComment);
				jQuery("#empId_select").val(timecard.empId).trigger('liszt:updated');
				jQuery("#timecard_dialog").dialog({
					 width:500,
					 modal:true
				});
			} else {
				alert("内部错误");
			}
		}
	});
};

function confirmDelete(id) {
	jConfirm('你确定要删除这条信息吗?', '考勤信息', function(r) {
		if(r) {
			jQuery.ajax({
				   type: "post",
				   url: "/timecard/tableDelete",
				   dataType:"json",
				   data: {cardId: id},
				   success: function(data){
					   if(data.flag == true) {
						   	window.location.reload();
						   	jQuery.jGrowl("恭喜您，删除奖金成功！");
					   }else {
						   jQuery.jGrowl("很抱歉，删除奖金失败！");
					   }
				   },
					error:function(e) {  
						jQuery.jGrowl("很抱歉，删除奖金失败！");
			        }
				 });
		}
	});
	return false;
};